package com.caojing.haolemai.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.bean.ContentBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CaoJing on 2016/10/18.
 */
public class HomeDetailGvAdapter extends BaseAdapter {
    private List<ContentBean.ResultsBean.ResultsBean2> mlist;
    private Context mContext;

    public HomeDetailGvAdapter(List<ContentBean.ResultsBean.ResultsBean2> list, Context mContext) {
        this.mlist = list;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mlist != null ? mlist.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mlist != null ? mlist.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertview == null) {
            convertview = LayoutInflater.from(mContext).inflate(R.layout.home_detail_gv_item, null);
            holder = new ViewHolder(convertview);
            convertview.setTag(holder);
        } else {
            holder = (ViewHolder) convertview.getTag();
        }
        Glide.with(mContext).load(mlist.get(position).getListImage()).thumbnail(0.1f).centerCrop().placeholder(R.mipmap.global_default_image_outlets).error(R.mipmap.global_default_image_outlets).into(holder.homeDetailGvItemIv);
        holder.homeDetailGvItemTvName.setText(mlist.get(position).getName());
        holder.homeDetailGvItemTvNPrice.setText("￥"+mlist.get(position).getSalePrice().substring(0,mlist.get(position).getSalePrice().indexOf(".")));
        holder.homeDetailGvItemTvOPrice.setText("￥"+mlist.get(position).getMarketPrice().substring(0,mlist.get(position).getMarketPrice().indexOf(".")));
        holder.homeDetailGvItemTvDiscount.setText(mlist.get(position).getDiscount());
        return convertview;
    }


    static class ViewHolder {
        @Bind(R.id.home_detail_gv_item_iv)
        ImageView homeDetailGvItemIv;
        @Bind(R.id.home_detail_gv_item_tvName)
        TextView homeDetailGvItemTvName;
        @Bind(R.id.home_detail_gv_item_tvNPrice)
        TextView homeDetailGvItemTvNPrice;
        @Bind(R.id.home_detail_gv_item_tvOPrice)
        TextView homeDetailGvItemTvOPrice;
        @Bind(R.id.home_detail_gv_item_tvDiscount)
        TextView homeDetailGvItemTvDiscount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
